﻿using System.Collections.Generic;
using System.Linq;
using DroidClaim.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using System;
using DroidClaim.DAL.Models;


namespace DroidClaim.DAL.Dal
{
    public class ClaimInfoDAL
    {
        private readonly ISession _session;
        public ClaimInfoDAL(ISession session)
        {
             this._session = session;
        }

        public List<ClaimInfo> GetAll(ClaimInfoSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(ClaimInfo));

            if (searchCriteria != null)
            {
                // Search										
						if (!string.IsNullOrEmpty(searchCriteria.Code))
						{
							criteria.Add(Expression.Like("Code", searchCriteria.Code, MatchMode.Start));
						}
						
															
						if (!string.IsNullOrEmpty(searchCriteria.Name))
						{
							criteria.Add(Expression.Like("Name", searchCriteria.Name, MatchMode.Start));
						}

                        if (searchCriteria.Id > 0)
                        {
                            criteria.Add(Expression.Eq("Id", searchCriteria.Id));
                        }

                //Sort
                if (!string.IsNullOrEmpty(searchCriteria.Sort))
                {
                    if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                    {
                        criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                    }
                    
                }
            }

            List<ClaimInfo> result = criteria.List<ClaimInfo>().ToList();
            if (result==null || result.Count <= 0)
            {
                return result;
            }

            if (searchCriteria.Customer != null)
            {
                List<ClaimInfo> customerList = result.FindAll(a => a.Customer != null);
                result = customerList.FindAll(a => a.Customer.LicensePlate == searchCriteria.Customer.LicensePlate);
            }

            if (searchCriteria.Surveyor != null)
            {
                List<ClaimInfo> surveyorList = result.FindAll(a => a.Surveyor != null);
                result = surveyorList.FindAll(a => a.Surveyor.Username == searchCriteria.Surveyor.Username);
            }

            return result;
        }

        public void Add(ClaimInfo claiminfo)
        {
            _session.Save( claiminfo);
        }

        public void Update(ClaimInfo claiminfo)
        {
			 claiminfo = _session.Merge(claiminfo);
            _session.Update( claiminfo);
        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE ClaimInfo where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");

        }
    }
}

